package cn.chengpengper.nacos.client.convert;

import java.util.HashMap;
import java.util.Map;
import java.util.MissingFormatArgumentException;
import java.util.Objects;

public class CompositeConverter {

    private final Map<Class<?>, AbstractPropertyConverter<?>> converterRegistry = new HashMap<>();

    public CompositeConverter() {
        converterRegistry.put(Boolean.class, new BooleanConverter());
        converterRegistry.put(Boolean.class, new IntegerConverter());
        converterRegistry.put(Boolean.class, new LongConverter());
    }

    public <T> T convert(String property, Class<T> targetClass) {
        AbstractPropertyConverter<?> converter = converterRegistry.get(targetClass);
        if (Objects.isNull(converter)) {
            throw new MissingFormatArgumentException("converter not found, can't convert from String to " + targetClass.getCanonicalName());
        }
        return (T) converter.convert(property);
    }
}
